package com.amigo.online.provider.sync.handle.service.impl;

import java.util.Map;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.entity.UserMysqlInfo;
import com.amigo.online.provider.sync.abs.BasicMongoDataProjectProperties;
import com.amigo.online.provider.sync.convert.ConvertJsonObjectService;
import com.amigo.online.provider.sync.handle.data.mongo.service.UserMongoService;
import com.amigo.online.provider.sync.handle.service.AbstractDataHandler;
import com.amigo.online.provider.sync.handle.service.DataHandler;

/**
 * @version: V 1.0.0
 * @Description: （user 中的 mongo 数据处理）
 * @author: zxy QQ:1363320658
 * @date: 2018年12月11日 下午4:39:20
 */
@Service("userMongoDataHandler")
public class UserMongoDataHandler extends AbstractDataHandler implements DataHandler {

    @Autowired
    UserMongoService userMongoService;

    @Autowired
    BasicMongoDataProjectProperties basicMongoDataProjectProperties;

    @Autowired
    @Qualifier("mongoUserConvertServiceImpl")
    ConvertJsonObjectService<Document, JSONObject, UserMysqlInfo> mongoUserconvertJsonObjectService;

    @Override
    public void updateHandle(JSONObject newObject, JSONObject oldObjecet) {
        long id = newObject.getLongValue("id");
        Query query = new Query().addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        boolean updateFlag = false;
        long result = 0;
        Map<String, String> projectMap = basicMongoDataProjectProperties.getUserProjectMap();
        for (String temp : oldObjecet.keySet()) {
            if (basicMongoDataProjectProperties.getUserUpdateList().contains(temp)) {
                String projectColumn = projectMap.get(temp);
                if (projectColumn != null) {
                    update.set(projectColumn, newObject.get(temp));
                } else {
                    update.set(temp, newObject.get(temp));
                }
                update.set(temp, newObject.get(temp));
                updateFlag = true;
            }
        }
        if (updateFlag == true) {
            result = userMongoService.updateObject(query, update);
            if (result == 0) {
                logger.error("更新mongoUser表数据失败！更新数据ID: " + id);

                // kafkaMessageSender.FailLogSend("", "");
            }
        }
    }

    @Override
    public void insertHandle(JSONObject newObject) {
        Document userDoc = mongoUserconvertJsonObjectService.convert(newObject);
        userMongoService.insertObject(userDoc);
    }

    @Override
    public void updateInfoByIdThroughHashMap(Long id, Map<String, Object> updateMap) {
        Query query = new Query().addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        for (String temp : updateMap.keySet()) {
            update.addToSet(temp, updateMap.get(temp));
        }
        long result = userMongoService.updateObject(query, update);
        if (result == 0) {

        }
    }

    @Override
    public void insertObject(Object userMysqlInfo) {
        Document userDoc = mongoUserconvertJsonObjectService.convertBeanToObject((UserMysqlInfo) userMysqlInfo);
        userMongoService.insertObject(userDoc);

    }

}
